﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleInfrastructure.Log;
using SimpleInfrastructure.Message;
using SimpleInfrastructure.Helpers;


namespace SimpleInfrastructure.Exceptions
{
    public class DefaultExceptionPolicy:IExceptionPolicy
    {
        private readonly ILogger _logger;
        private readonly INotifier _notifier;
        public DefaultExceptionPolicy(ILogger logger, INotifier notifier)
        {
            _logger = logger;
            _notifier = notifier;
        }
        public bool HandleException(object sender, Exception exception)
        {
            if (exception.IsFatal())
            {
                return false;
            }
            var aex = exception as AlertException;
            string amessage = aex != null ? aex.AlertMessage : exception.Message;
            _logger.Log(LogLevel.Error, exception, "An unexpected exception was caught");
            _notifier.Notify("",EnvHelper.GetClientID(), amessage,NotifyType.Error);
            return true;
        }
    }
}
